public class Josephus {

    public static void main(String[] args) {
        int n = 15;
        int m = 3;
        josephusMath(n, m);
        System.out.println();
        josephusLink(n, m);
    }

    private static void josephusLink(int n, int m) {
        /*n个人站好队，开始数数，数到m时出局，出局之后，从新0数到m*/
        Person firstPerson = new Person();
        firstPerson.value = 1;
        firstPerson.next = firstPerson;

        Person temp = firstPerson;

        for (int index = 2; index < n + 1; index++) {
            Person person = new Person();
            person.value = index;
            temp.next = person;
            temp = person;
        }
        temp.next = firstPerson;

        /*开始杀人*/
        temp = firstPerson;
        int flag = 0;
        Person last = temp;
        while (temp != temp.next) {
            flag++;
            if (flag == m) {
                last.next = temp.next;
                flag = 0;
            } else {
                last = temp;
            }
            temp = temp.next;
        }

        System.out.println("最后剩下：" + temp.value);
    }


    public static class Person {
        protected int value;
        protected Person next;
    }

    private static void josephusMath(int n, int m) {
        int live = 0;
        for (int index = 2; index <= n; index++) {
            live = (live + m) % index;
        }
        System.out.print("最后剩下：" + (live + 1));
    }

}